﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Gao.Poker.App.UserControls;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;

namespace Gao.Poker.App.Win
{
    /// <summary>
    /// Interaction logic for CompetitionWin.xaml
    /// </summary>
    public partial class CompetitionWin : Window
    {
        private readonly CompetitionViewModel _viewModel;

        public CompetitionWin()
        {
            InitializeComponent();
            _viewModel = new CompetitionViewModel{Message = OnMessage};
            DataContext = _viewModel;
            btnNew.Click += New_Click;
        }

        private void SaveCompetition_Click(Object sender, RoutedEventArgs e)
        {
            List<Tables> list = lvTables.SelectedItems.OfType<Tables>().ToList();

            CompetitionService service = new CompetitionService();
            _viewModel.Competition.TablesNum = String.Join(",",list.Select(a => a.Number).ToArray());
            _viewModel.Competition = service.Save(_viewModel.Competition,list);


            _viewModel.OnPropertyChanged("Competitions");
            OnMessage("保存成功");
        }

        void New_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Competition = new Competition();
        }

        private void OnMessage(string msg)
        {
            MessageBox.Show(msg, "提示信息");
        }
    }
}
